package com.cg.controller;

import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.User;
import com.cg.service.UserService;
import com.cg.vo.LoginUserVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author chengong
 * @since 2024-01-14 18:06:01
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 添加新用户
     */
    @PostMapping("/createUser")
    public Result createUser(@RequestBody User user){
        Integer count = userService.createUser(user);
        if(count.equals(0)){
            return ResultUtils.returnResult(ResultEnum.FAIL_OPERATE);
        }
        return ResultUtils.returnSuccess();
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginUserVo> login(@RequestBody User user){
        return ResultUtils.returnDataSuccess(userService.login(user));
    }
}

